---
category: Number
created: '2020-05-16'
title: Round a number to a given number of digits
updated: '2021-10-13'
---

**JavaScript version**

```js round.js
const round = (n, decimals = 0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`);
```

**TypeScript version**

```ts round.ts
const round = (n: number, decimals: number = 0): number => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`);
```

**Examples**

```js examples.js
round(1.234567, 3); // 1.235
round(1.234567, 4); // 1.2346
```
